package com.renwei.yunlong.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.DetailsBean;
import com.renwei.yunlong.event.SparePageRefreshEvent;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ServiceRequestManager;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 备件申请相关  明细  页面
 * <p>
 * Created by Administrator on 2017/9/7.
 */

public class SpareDetailsFragment extends BaseLazyFragment implements HttpTaskListener {

    @Bind(R.id.tv_application)
    TextView tvApplication;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_enterprise)
    TextView tvEnterprise;
    @Bind(R.id.tv_service_new)
    TextView tvServiceNew;
    @Bind(R.id.tv_service)
    TextView tvService;
    @Bind(R.id.ll_service)
    RelativeLayout llService;
    @Bind(R.id.tv_third_party)
    TextView tvThirdParty;
    @Bind(R.id.ll_thrid)
    RelativeLayout llThrid;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_select_spare)
    TextView tvSelectSpare;
    @Bind(R.id.tv_replace)
    TextView tvReplace;
    @Bind(R.id.tv_describe)
    TextView tvDescribe;
    @Bind(R.id.tv_use)
    TextView tvUse;
    private String sprId;

    public SpareDetailsFragment(String sprId) {
        this.sprId = sprId;
    }

    public SpareDetailsFragment() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_spare_application_details;
    }

    @Override
    public void onLazyLoad() {
        EventBus.getDefault().register(this);
        initView();
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(SparePageRefreshEvent event) {
        initData();
    }

    public void initView() {
        if ("1".equals(companyType)) {
            llService.setVisibility(View.GONE);
            llThrid.setVisibility(View.GONE);
        } else if ("2".equals(companyType)) {
            llService.setVisibility(View.VISIBLE);
            llThrid.setVisibility(View.VISIBLE);
        }
    }


    public void initData() {
        ServiceRequestManager.getManager().getSpareDetail(getContext(), sprId, this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                DetailsBean serviceBean = new Gson().fromJson(result, DetailsBean.class);
                if (serviceBean.getMessage().getCode() == 200) {
                    tvApplication.setText(serviceBean.getRows().getApplyer());//申请人
                    tvPhone.setText(serviceBean.getRows().getMobilePhone());//手机号
                    tvEnterprise.setText(serviceBean.getRows().getCompanyName());//所属企业

                    if (TextUtils.isEmpty(serviceBean.getRows().getAssetSerial())) {
                        tvServiceNew.setText(TextUtils.isEmpty(serviceBean.getRows().getAssetName()) ? "无" : serviceBean.getRows().getAssetName()
                                + "/" + "无" + "/" + "无");//服务对象
                    } else {
                        tvServiceNew.setText(TextUtils.isEmpty(serviceBean.getRows().getAssetName()) ? "无" : serviceBean.getRows().getAssetName()
                                + "/" + serviceBean.getRows().getAssetSerial() + "/" + serviceBean.getRows().getAssetSerial());//服务对象
                    }
                    tvService.setText(serviceBean.getRows().getIsBackUp());//备件服务
                    tvThirdParty.setText(serviceBean.getRows().getIsTurnFlag());
                    if (!TextUtils.isEmpty(serviceBean.getRows().getArriveTime())) {
                        String[] times = serviceBean.getRows().getArriveTime().split("T");
                        tvTime.setText(times[0] + " " + times[1]);
                    }
                    tvDescribe.setText(serviceBean.getRows().getTitle());
                    tvSelectSpare.setText((TextUtils.isEmpty(serviceBean.getRows().getSprHardName()) ? "无" : serviceBean.getRows().getSprHardName())
                            + "/" + (TextUtils.isEmpty(serviceBean.getRows().getSprHardSerial()) ? "无" : serviceBean.getRows().getSprHardSerial())
                            + "/" + (TextUtils.isEmpty(serviceBean.getRows().getSprHardwareTypeName()) ? "无" : serviceBean.getRows().getSprHardwareTypeName()));

                    if ("1".equals(serviceBean.getRows().getTypeFlag())){
                        tvUse.setText("新增");
                    }else if("2".equals(serviceBean.getRows().getTypeFlag())){
                        tvUse.setText("替换");
                    }

                    if ("2".equals(serviceBean.getRows().getOldReturnFlag())) {
                        tvReplace.setText("不处理");
                    } else if ("1".equals(serviceBean.getRows().getOldReturnFlag())) {
                        tvReplace.setText("入库");
                    } else if ("3".equals(serviceBean.getRows().getOldReturnFlag())) {
                        tvReplace.setText("原厂");
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }

}
